package com.atXiao.food.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.atXiao.food.entity.WeatherResponse;
import com.atXiao.food.rspbody.weather.WeatherItem;
import com.atXiao.food.rspbody.weather.WeatherRespBody;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class WeatherApiClient {
    public static void main(String[] args) {
        try {
            // 定义接口地址和城市编号
            String apiUrl = "http://t.weather.sojson.com/api/weather/city/";
            String cityCode = "101110101";  // 西安市

            // 拼接完整的请求URL
            String requestUrl = apiUrl + cityCode;

            // 创建 URL 对象
            URL url = new URL(requestUrl);

            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应内容
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                WeatherRespBody weatherRespBody = new WeatherRespBody();
                Object shidu = JSONPath.read(response.toString(), "$.data.shidu");
                weatherRespBody.setShidu((String) shidu);
                Object pm25 = JSONPath.read(response.toString(), "$.data.pm25");
                weatherRespBody.setPm25((BigDecimal) pm25);
                Object pm10 = JSONPath.read(response.toString(), "$.data.pm10");
                weatherRespBody.setPm10((BigDecimal) pm10);
                Object quality = JSONPath.read(response.toString(), "$.data.quality");
                weatherRespBody.setQuality((String) quality);
                Object wendu = JSONPath.read(response.toString(), "$.data.wendu");
                weatherRespBody.setWendu((String) wendu);
                Object ganmao = JSONPath.read(response.toString(), "$.data.ganmao");
                weatherRespBody.setGanmao((String) ganmao);
                // 天气数据
                JSONArray weatherList = (JSONArray) JSONPath.read(response.toString(), "$.data.forecast");
                int tempIndex=0;
                ArrayList<WeatherItem> weatherRspList = new ArrayList<>();
                for (Object weatherObject : weatherList) {
                    if (tempIndex>3){
                        break;
                    }
                    WeatherItem weatherItem = new WeatherItem();
                    JSONObject weather= (JSONObject) weatherObject;
                    // 日期
                    Object date = weather.get("date");
                    weatherItem.setDate((String) date);
                    Object high = weather.get("high");
                    weatherItem.setHigh((String) high);
                    Object low = weather.get("low");
                    weatherItem.setLow((String) low);
                    Object ymd = weather.get("ymd");
                    weatherItem.setYmd((String) ymd);
                    Object week = weather.get("week");
                    weatherItem.setWeek((String) week);
                    Object sunrise = weather.get("sunrise");
                    weatherItem.setSunrise((String) sunrise);
                    Object sunset = weather.get("sunset");
                    weatherItem.setSunset((String) sunset);
                    Object type = weather.get("type");
                    weatherItem.setType((String) type);
                    Object notice = weather.get("notice");
                    weatherItem.setNotice((String) notice);
                    weatherRspList.add(weatherItem);
                    tempIndex++;
                }
                weatherRespBody.setWeatherList(weatherRspList);
                System.out.println("当前响应数据:"+JSONObject.toJSONString(weatherRespBody));
            } else {
                System.out.println("HTTP GET请求失败，响应码：" + responseCode);
            }


            // 关闭连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
